home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gchess40.lha / gnuchess4.0p62 / src / gdxbkstats.c < prev    next >
C/C++ Source or Header  |  1993-06-22  |  3KB  |  167 lines

  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #include "gnuchess.h"
  6.  
  7. /* print stats for gdx book also show moves/hints for a position (-h option)
  8. and allow counts to be modified. Modifying counts will effect how often
  9. a line is choosen since its done in proportion to the counts */
  10.  
  11. #undef rxx
  12. #undef cxx
  13. #undef scanz
  14. #undef printz
  15.  
  16. #define rxx "12345678"
  17. #define cxx "abcdefgh"
  18. char xmvstr[12];
  19. char incnt[12];
  20. char *
  21. cvt (m)
  22.      unsigned int m;
  23. {
  24.     unsigned int f, t;
  25.     f = m >> 8 & 0x3f;
  26.     t = m & 0x3f;
  27. /* algebraic notation */
  28.     xmvstr[0] = cxx[column (f)];
  29.     xmvstr[1] = rxx[row (f)];
  30.     xmvstr[2] = cxx[column (t)];
  31.     xmvstr[3] = rxx[row (t)];
  32.     xmvstr[6] = '\0';
  33.     if (m & DONTUSE)
  34.     xmvstr[4] = '?';
  35.     else
  36.     xmvstr[4] = ' ';
  37.     if (m & LASTMOVE)
  38.     xmvstr[5] = '*';
  39.     else
  40.     xmvstr[5] = ' ';
  41.     return xmvstr;
  42. }
  43.  
  44. #define lts(x) (x>>16)        /* long to short to convert hashkey to short */
  45. int gfd;
  46. struct gdxadmin
  47. {
  48.     unsigned int bookcount;
  49.     unsigned int booksize;
  50.     unsigned long maxoffset;
  51. } ADMIN;
  52.  
  53. #define N 2
  54. struct gdxdata
  55. {
  56.     unsigned int hashbd;
  57.     unsigned short hashkey;
  58.     unsigned short bmove;
  59.     unsigned short hint;
  60.     unsigned short count;
  61. } DATA;
  62. void 
  63. usage (char *x)
  64. {
  65.     printf ("usage %s binbookfile [ -h key bd]\n", x);
  66.     exit ();
  67. }
  68.  
  69. int i;
  70. int c1 = 0;
  71. int in = 0;
  72. int max = -9999;
  73. int min = 9999999;
  74. int n = 0;
  75. int sum = 0;
  76. int sumc = 0;
  77. unsigned long key, bd;
  78. int hk = false;
  79. main (argc, argv)
  80.      int argc;
  81.      char **argv;
  82. {
  83.     if (argc == 5)
  84.       {
  85.       if (strcmp (argv[2], "-h") != 0)
  86.           usage (argv[0]);
  87.       key = strtol (argv[3], NULL, 16);
  88.       bd = strtol (argv[4], NULL, 16);
  89. printf("key = %lx bd = %lx\n",key,bd);
  90.       hk = true;
  91.       }
  92.     else if (argc != 2)
  93.     usage (argv[0]);
  94.     if(hk)gfd = open (argv[1], O_RDWR);
  95.     else gfd = open (argv[1], O_RDONLY);
  96.     if (gfd >= 0)
  97.       {
  98.       read (gfd, &ADMIN, sizeof (struct gdxadmin));
  99.       printf ("entrysize %d\nbooksize %d\nbookcount %d\nmaxoffset %ld\n", sizeof (struct gdxdata), ADMIN.booksize, ADMIN.bookcount, ADMIN.maxoffset);
  100.       for (i = 0; i < ADMIN.booksize; i++)
  101.         {
  102.         if (0 > read (gfd, &DATA, sizeof (struct gdxdata)))
  103.           {
  104.               perror ("fread");
  105.               exit (1);
  106.           }
  107.         if (hk)
  108.           {
  109.               if (DATA.count && DATA.hashbd == bd && lts (key) == DATA.hashkey)
  110.             {
  111.                 int inc;
  112.                 unsigned long pos;
  113.                 printf ("%s ", cvt (DATA.bmove));
  114.                 printf ("%s ", cvt (DATA.hint));
  115.                 printf ("%d\n", DATA.count);
  116.                 printf("new count?");
  117.                 gets(incnt);
  118.                 inc = strtol(incnt,0,10);
  119.                 if(inc) {
  120.                 DATA.count = strtol(incnt,0,10);
  121.                 pos = sizeof(struct gdxadmin) + i * sizeof(struct gdxdata);
  122.                     lseek(gfd,pos,SEEK_SET);
  123.                     if(write(gfd,&DATA,sizeof(struct gdxdata)) < 0)
  124.                 {perror("write");exit();}
  125.                 }
  126.             }
  127.     
  128.           }
  129.         sumc += DATA.count;
  130.         if (in && DATA.bmove)
  131.           {
  132.               c1++;
  133.           }
  134.         else if (DATA.bmove)
  135.           {
  136.               in = 1;
  137.               c1 = 1;
  138.           }
  139.         else if (c1)
  140.           {
  141.               /*printf ("out %d\n", c1);*/
  142.               n++;
  143.               if (c1 < min)
  144.               min = c1;
  145.               if (c1 > max)
  146.               max = c1;
  147.               sum += c1;
  148.               c1 = 0;
  149.               in = 0;
  150.           }
  151.         }
  152.       close (gfd);
  153.       if (in)
  154.         {
  155.         /*printf ("out %d\n", c1);*/
  156.         n++;
  157.         if (c1 < min)
  158.             min = c1;
  159.         if (c1 > max)
  160.             max = c1;
  161.         sum += c1;
  162.         }
  163.       printf ("max %d\nmin %d\navg %f\nsumc %d\n", max, min, (float) sum / (float) n, sumc);
  164.  
  165.       }
  166. }
  167.